home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / c_news / 16 / sets / setsourc / setunion.c < prev    next >
C/C++ Source or Header  |  1989-03-09  |  2KB  |  69 lines

  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #include "sets.h"
  4. /***************************************************************************/
  5.              int set_union(set *xunion, set *set1, set *set2)
  6. /***************************************************************************/
  7. /* This procedure computes the union (set-sum) of two sets.  The sets must
  8.    have the same base_type in order for this to work.  The input parameters
  9.    are left unchanged.
  10. */
  11. {
  12. int i;
  13. defset(temp,UNIVERSAL,MAX_MEMBERS,0);
  14. set *temp2,*temp3;
  15.  
  16.     /* first check for the same base_types */
  17.     if(!cmp_base_types(set1,set2) || !cmp_base_types(set1,xunion) ||
  18.        !cmp_set_tags(set1,set2)   || !cmp_set_tags(set1,xunion))
  19.         return FAILURE;
  20.  
  21.     /* next clear the output set */
  22.     set_clear(&temp);
  23.  
  24.     /* The union of two sets is a set whose members are all the members of
  25.        both the sets.  The union is a set having the same base_type as
  26.        the two adding sets and having a set_size equal to the largest
  27.        set_size of the two sets.
  28.     */
  29.  
  30.     /* Assign the largest input set to the output set then add the smaller
  31.        set to the output.  Get the set with the smallest set_size
  32.     */
  33.     temp2 = (set1->set_size > set2->set_size) ? set2 : set1;
  34.     if(temp2 == set1)
  35.         temp3 = set2;
  36.     else
  37.         temp3 = set1;
  38.  
  39.     /* temp2 points to the smallest set, temp3, the larger.  The set,
  40.        'temp' must be at least the size of temp3.  Check this.
  41.     */
  42.     if(temp.member_recs < temp3->member_recs)
  43.         return FAILURE;
  44.  
  45.     /* do the assignment */
  46.     if(set_assign(&temp, temp3) == FAILURE)
  47.         return FAILURE;
  48.  
  49.     /* Add temp2 to union by bitwise-oring the respective member words of the
  50.        two sets and assign the result to the output set, union.  Note that
  51.        the smaller set will have ALL bits at member locations higher than
  52.        nmembers reset to zero.
  53.     */
  54.     for(i=0;i<temp2->member_recs;i++)
  55.         temp.word[i] = temp.word[i] | temp2->word[i];
  56.  
  57.     /* correct the member count in the set record */
  58.     temp.nmembers = set_member_count(&temp);
  59.  
  60.     /* now copy temp to the output set */
  61.     if(set_assign(xunion,&temp) == FAILURE)
  62.         return FAILURE;
  63.  
  64.     /* done. */
  65.     return SUCCESS;
  66.  
  67. }  /* end set_union */
  68.  
  69.